//
//  DKAuthSelectAlertView.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/19.
//

#import "DKAuthSelectAlertView.h"
@interface DKAuthSelectAlertView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, weak)zhPopupController * dk_AuthSelectAlertPop;
@property (nonatomic,strong)DKAuthSelectAlertView *dk_AuthSelectAlertView;
@property (nonatomic, strong)UIPickerView * authSelectPickView;
@property (nonatomic, copy)void(^SureSelectAuthBlock)(DKAuthCommonDicModel *authCommonDicMdl);
@property (nonatomic, strong)NSArray * commonDicMdls;
@end
@implementation DKAuthSelectAlertView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.whiteColor;
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    UIButton * cancleBtn = [[UIButton alloc] init];
    [cancleBtn setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:DK_Hex(@"#999999") forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = DK_FONT_Regular(15);
    [cancleBtn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancleBtn];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(39)));
        make.top.equalTo(@(DK_IsWidthScale(9)));
        make.height.equalTo(@(DK_IsWidthScale(48)));
    }];
    UIButton * confirmBtn = [[UIButton alloc] init];
    [confirmBtn setTitle:@" Confirm" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = DK_FONT_Semibold(15);
    [confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DK_IsWidthScale(35)));
        make.top.equalTo(@(DK_IsWidthScale(9)));
        make.height.equalTo(@(DK_IsWidthScale(48)));
    }];
    UIView *authSelectLineView = [[UIView alloc] init];
    authSelectLineView.backgroundColor = DK_Hex(@"#ECECEC");
    [self addSubview:authSelectLineView];
    [authSelectLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(57)));
        make.height.equalTo(@(DK_IsWidthScale(1)));
    }];
    self.authSelectPickView = [[UIPickerView alloc] init];
    self.authSelectPickView.delegate = self;
    self.authSelectPickView.dataSource = self;
    [self addSubview:self.authSelectPickView];
    [self.authSelectPickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(authSelectLineView.mas_bottom).offset(DK_IsWidthScale(0));
        make.bottom.equalTo(@(-DK_SafeAreaBottom));
    }];
    
}
- (void)cancle{
    [_dk_AuthSelectAlertPop dismiss];
}
- (void)confirm{
    [_dk_AuthSelectAlertPop dismiss];
    NSInteger index = [self.authSelectPickView selectedRowInComponent:0];
    DKAuthCommonDicModel * authCommonDicMdl = self.commonDicMdls[index];
    if(self.SureSelectAuthBlock){
        self.SureSelectAuthBlock(authCommonDicMdl);
    }
}
-(void)setCommonDicMdls:(NSArray *)commonDicMdls{
    _commonDicMdls = commonDicMdls;
    [self.authSelectPickView reloadAllComponents];
}
-(void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self makeCornerOnTopleftRight:DK_IsWidthScale(50)];
}
+ (void)dk_ShowAuthSelectAlertView:(NSArray*)commonDicMdls complete:(void(^)(DKAuthCommonDicModel *authCommonDicMdl))complete{
    DKAuthSelectAlertView * dk_AuthSelectAlertView = [[DKAuthSelectAlertView alloc] init];
    zhPopupController * sureAuthStartCtrl = [[zhPopupController alloc] initWithView:dk_AuthSelectAlertView size:CGSizeMake(DK_SCREEN_W, DK_IsWidthScale(320))];
    dk_AuthSelectAlertView.SureSelectAuthBlock = complete;
    dk_AuthSelectAlertView.commonDicMdls = commonDicMdls;
    sureAuthStartCtrl.presentationStyle = zhPopupSlideStyleTransform;
    sureAuthStartCtrl.layoutType = zhPopupLayoutTypeBottom;
    sureAuthStartCtrl.presentationTransformScale = 0.56;
    sureAuthStartCtrl.dismissonTransformScale = 0.56;
    sureAuthStartCtrl.dismissOnMaskTouched = NO;
    dk_AuthSelectAlertView.dk_AuthSelectAlertPop = sureAuthStartCtrl;
    [sureAuthStartCtrl showInView:((AppDelegate*)([UIApplication sharedApplication].delegate)).window completion:^{
        
    }];
}
#pragma mark -- UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component API_UNAVAILABLE(tvos){
    return DK_IsWidthScale(50);;
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.commonDicMdls.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    DKAuthCommonDicModel * authCommonDicMdl = self.commonDicMdls[row];
    return authCommonDicMdl.remark;
}
@end
